﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.PersonalProjectManagement
{
    public partial class EditPersonalProject : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("XSZNSYXMLB");
            strPageUserType.Add("mana");
            if (Request.QueryString["ProjectID"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            int ProjectID = 0;
            if (!int.TryParse(Request.QueryString["ProjectID"], out ProjectID))
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
                return;
            }
            if (!IsPostBack)
            {
                LabDropdownlist();
                BindApplicantList(ProjectID);
                BindEquipList(ProjectID);
                BindData(ProjectID);

                LabDate.Attributes.Add("onclick", "d.showMoreDay = false;d.show(this);");
                LabDate.Attributes.Add("readOnly", "-1");
            }
        }

        //绑定所属实验室
        protected void LabDropdownlist()
        {
            LabMS.BLL.Lab lab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> ParLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> SonLab = new List<LabMS.Model.Lab>();
            List<LabMS.Model.Lab> LabTemp = new List<LabMS.Model.Lab>();

            ParLab = lab.GetModelList("Lab_ParentID is null");
            SonLab = lab.GetModelList("Lab_ParentID is not null");
            LabTemp = lab.GetModelList("Lab_ParentID is null");

            int count = ParLab.Count, count1 = SonLab.Count;
            for (int i = 0; i < count; i++)
            {
                int ID = ParLab[i].ID;
                int m = 0;
                for (int j = 0; j < count1; j++)
                {
                    LabMS.Model.Lab labModel = new LabMS.Model.Lab();
                    int ParentID = int.Parse(SonLab[j].Lab_ParentID.ToString());

                    if (ParentID == ID)
                    {
                        labModel.ID = SonLab[j].ID;
                        labModel.Lab_Name = "|-" + SonLab[j].Lab_Name;
                        LabTemp.Insert(i + m + 1, labModel);
                        m++;
                    }
                }
            }
            ddlApplyLab.DataSource = LabTemp;
            ddlApplyLab.DataBind();
            ListItem item = new ListItem("无", "0");
            ddlApplyLab.Items.Insert(0, item);
        }

        //绑定实验室用房
        protected void LocationDropdownList(int LabID)
        {
            LabMS.BLL.Location location = new LabMS.BLL.Location();
            string strWhere = " LabID=" + LabID;
            DataSet ds = new DataSet();

            ds = location.GetList(strWhere);
            ddlLocation.DataSource = ds;
            ddlLocation.DataBind();
            ddlLocation.Items.Insert(0, new ListItem("", "0"));
        }
        //绑定申请人
        protected void BindApplicantList(int ProjectID)
        {
            ExtendBLL.PersonalProject personalproject = new ExtendBLL.PersonalProject();
            List<ExtendModule.StudentAndOther> applicantList = new List<ExtendModule.StudentAndOther>();

            string strWhere = " pa.PersonalProjID = " + ProjectID;
            applicantList = personalproject.GetStudentInfoList(strWhere);
            gvApplicant.DataSource = applicantList;
            gvApplicant.DataBind();
        }
        //绑定仪器及低耗品列表
        protected void BindEquipList(int ProjectID)
        {
            LabMS.BLL.PersonalProjConsume consume = new LabMS.BLL.PersonalProjConsume();
            DataSet equipList = new DataSet();
            DataSet consumeList = new DataSet();

            string strWhere1 = " EquipType=1 and PersonalProjID= " + ProjectID;
            string strWhere2 = " EquipType=2 and PersonalProjID= " + ProjectID;

            equipList = consume.GetList(strWhere1);
            gvEquip.DataSource = equipList;
            gvEquip.DataBind();

            consumeList = consume.GetList(strWhere2);
            gvLowConsumption.DataSource = consumeList;
            gvLowConsumption.DataBind();
        }

        //绑定申请信息
        protected void BindData(int ProjectID)
        {
            LabMS.BLL.PersonalProject perProject = new LabMS.BLL.PersonalProject();
            LabMS.Model.PersonalProject perProjectInfo = new LabMS.Model.PersonalProject();

            perProjectInfo = perProject.GetModel(ProjectID);
            ProjectName.Text = perProjectInfo.ProjectName;
            PlanHours.Text = perProjectInfo.PlanHours.ToString();
            ddlApplyLab.SelectedValue = perProjectInfo.AppcationLabID.ToString();
            if (!string.IsNullOrEmpty(perProjectInfo.ApplicationStartTime.ToString()))
            {
                ApplyTimeMin.Text = perProjectInfo.ApplicationStartTime.Value.ToShortDateString();
            }
            if (!string.IsNullOrEmpty(perProjectInfo.ApplicationEndTime.ToString()))
            {
                ApplyTimeMax.Text = perProjectInfo.ApplicationEndTime.Value.ToShortDateString();
            }

            string strSemester = Common.Status.GetSemester(perProjectInfo.Semester.Value);
            lbSTime.Text = perProjectInfo.SYear.Value.ToString() + "学年 " + strSemester;
            lbStatus.Text = Common.Status.GetPersonalProjectStatus(perProjectInfo.ProjectStatus.ToString());

            Contactor.Text = perProjectInfo.Contactor;
            ContactorClass.Text = perProjectInfo.ContactorClass;
            ContactEmail.Text = perProjectInfo.ContactorEmail;
            ContactPhone.Text = perProjectInfo.ContactorPhone;
            Teacher.Text = perProjectInfo.Teacher;
            TeacherTitle.Text = perProjectInfo.TeacherTitle;
            TeacherEmail.Text = perProjectInfo.TeacherEmail;
            TeacherPhone.Text = perProjectInfo.TeacherPhone;
            ApplyReason.Text = perProjectInfo.ApplicationReason;
            Opinion.Text = perProjectInfo.Opinion;
            if (!string.IsNullOrEmpty(perProjectInfo.ProjectStartTime.ToString()))
            {
                LabDate.Text = perProjectInfo.ProjectStartTime.Value.ToShortDateString();
                LabStartTimeHour.Text = perProjectInfo.ProjectStartTime.Value.Hour.ToString();
                LabStartTimeMinute.Text = perProjectInfo.ProjectStartTime.Value.Minute.ToString();
            }
            if (!string.IsNullOrEmpty(perProjectInfo.ProjectEndTime.ToString()))
            {
                LabEndTimeHour.Text = perProjectInfo.ProjectEndTime.Value.Hour.ToString();
                LabEndTimeMinute.Text = perProjectInfo.ProjectEndTime.Value.Minute.ToString();
            }

            int LabID = int.Parse(perProjectInfo.AppcationLabID.ToString());
            LocationDropdownList(LabID);
            ddlLocation.SelectedValue = perProjectInfo.ProjectLabID.ToString();
        }

        //审批[通过]
        protected void btnApprove_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(Opinion.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "审批意见不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(LabDate.Text.Trim()) || string.IsNullOrEmpty(LabStartTimeHour.Text.Trim()) || string.IsNullOrEmpty(LabStartTimeMinute.Text.Trim())
                || string.IsNullOrEmpty(LabEndTimeHour.Text.Trim()) || string.IsNullOrEmpty(LabEndTimeMinute.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "实验时间不能为空!");
                return;
            }

            int StartHour = 0;
            int EndHour = 0;
            int StartMinute = 0;
            int EndMinute = 0;
            if (!(int.TryParse(LabStartTimeHour.Text.Trim(), out StartHour) && int.TryParse(LabStartTimeMinute.Text.Trim(), out StartMinute)
                && int.TryParse(LabEndTimeHour.Text.Trim(), out EndHour) && int.TryParse(LabEndTimeMinute.Text.Trim(), out EndMinute)))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "实验时间只能输入数字!");
                return;
            }
            else
            {
                StartHour = int.Parse(LabStartTimeHour.Text.Trim());
                StartMinute = int.Parse(LabStartTimeMinute.Text.Trim());
                EndHour = int.Parse(LabEndTimeHour.Text.Trim());
                EndMinute = int.Parse(LabEndTimeMinute.Text.Trim());

                if (StartHour > 24 || EndHour > 24 || StartMinute > 60 || EndMinute > 60)
                {
                    LabMS.Common.JShelper.JSAlert(Page, "err", "实验时间不合法!");
                    return;
                }
                if (StartHour > EndHour || ((StartHour.Equals(EndHour)) && (StartMinute>=EndMinute)))
                {
                    LabMS.Common.JShelper.JSAlert(Page, "err", "实验开始时间大于结束时间!");
                    return;
                }     
            }
            if (ddlLocation.SelectedValue.ToString().Equals("0"))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "实验地点不能为空!");
                return;
            }
            #endregion
            LabMS.BLL.PersonalProject projectinfo = new LabMS.BLL.PersonalProject();
            LabMS.Model.PersonalProject project = new LabMS.Model.PersonalProject();

            int ProjectID = int.Parse(Request.QueryString["ProjectID"]);
            project = projectinfo.GetModel(ProjectID);

            project.Opinion = Opinion.Text;
            project.ProjectLabID = int.Parse(ddlLocation.SelectedValue.Trim());
            project.ProjectStartTime = DateTime.Parse(LabDate.Text + "  " + LabStartTimeHour.Text + ":" + LabStartTimeMinute.Text + ":00");
            project.ProjectEndTime = DateTime.Parse(LabDate.Text + "  " + LabEndTimeHour.Text + ":" + LabEndTimeMinute.Text + ":00");
            project.ProjectStatus = "4";
            try
            {
                projectinfo.Update(project);
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }
            LabMS.Common.JShelper.JSAlertAndRedirect(Page, "ApproveSuccess", "审批通过操作成功", "ListPersonalProject.aspx");
        }

        //审批[不通过]
        protected void btnUnapprove_Click(object sender, EventArgs e)
        {
            #region 验证
            if (string.IsNullOrEmpty(Opinion.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "审批意见不能为空!");
                return;
            }
            #endregion
            LabMS.BLL.PersonalProject projectinfo = new LabMS.BLL.PersonalProject();
            LabMS.Model.PersonalProject project = new LabMS.Model.PersonalProject();

            int ProjectID = int.Parse(Request.QueryString["ProjectID"]);
            project = projectinfo.GetModel(ProjectID);

            project.Opinion = Opinion.Text;
            project.ProjectStatus = "3";
            try
            {
                projectinfo.Update(project);
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }
            LabMS.Common.JShelper.JSAlertAndRedirect(Page, "UnapproveSuccess", "审批不通过操作成功", "ListPersonalProject.aspx");
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            int ProjectID = int.Parse(Request.QueryString["ProjectID"]);
            string strAlert = "删除成功!";
            try
            {
                LabMS.BLL.PersonalProject project = new LabMS.BLL.PersonalProject();
                project.Delete(ProjectID);

                ExtendBLL.PersonalProject consume = new ExtendBLL.PersonalProject();
                consume.DeleteConsumeByProjectID(ProjectID);

                ExtendBLL.PersonalProject applicant = new ExtendBLL.PersonalProject();
                applicant.DeleteApplicantByProjectID(ProjectID);
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
                return;
            }

            Common.JShelper.JSAlertAndRedirect(Page, "success", strAlert, "ListPersonalProject.aspx");
        }

    }
}
